<?
$tplContact=new TplLoad;
$lSessionStarted=1;
$tplContact->assign("phpself",$_SERVER['PHP_SELF']);
if ($set_contact_login_need)
	check_valid_user("");

// Refereer information
$legal = 0;
$lServerRef = explode("/",getenv("HTTP_REFERER"));
$lUserid=getParamInt("user_id","");
$tplContact->assign("ad_owner",getParam("ad_owner",""));
$tplContact->assign("id",getParamInt("id",""));
$tplContact->assign("id",getParamInt("id",""));
$tplContact->assign("user_id",getParamInt("user_id",""));
if ($set_img_validation==1)
		$tplContact->assign("image_validation",1);

		
//$lAdId=getParam("id","");		
if ($lUserid=="") // If this is an ad, lookup the user_id
{
	$sql="select ad_title,ad_id,ad_owner from $ads_tbl where ad_id='".getParamInt("id","")."' limit 1";
	$res_title=q($sql);
	$row_title=mysql_fetch_array($res_title);
	$lAdTitle=$row_title["ad_title"];
	$lAdId=$row_title["ad_id"];
	$lUserid=$row_title["ad_owner"];		
}

if ($lUserid!="") // If a userid is found, get name, email etc. of the person you try to contact
{
	// Owner of this ad
	$lSqlGetOwner = "select user_name,user_email from $usr_tbl where user_id = $lUserid limit 1";
	$lResultOwner = q ($lSqlGetOwner);
	$lRowContact = mysql_fetch_array($lResultOwner);
	$lAdOwnerName=$lRowContact["user_name"];
	$lAdOwnerEmail=$lRowContact["user_email"];
	$tplContact->assign("ad_owner",strEnc($lRowContact["user_name"]));
}


if (!$lAdId && getParamInt("id","")!="")
{
	$lAdId=getParamInt("id","");
	$sql="select ad_title,ad_id,ad_owner from $ads_tbl where ad_id=$lAdId limit 1";
	$res_title=q($sql);
	$row_title=mysql_fetch_array($res_title);
	$lAdTitle=$row_title["ad_title"];
	$lAdId=$row_title["ad_id"];
	$lUserid=$row_title["ad_owner"];		
	
}

if (getParam("submit",""))
{
		$errorStr="";
		
		// Person contacting the ad owne
		$tplContact->assign("contact_name",strEnc(getParam("contact_name","")));
		$tplContact->assign("contact_email",strEnc(getParam("contact_email","")));
		$tplContact->assign("contact_message",strEnc(getParam("contact_message","")));
		
		if (empty($lUserid))
			$errorStr.="<li>".LA_ERROR_MSG5."</li>";
			
		if (!validEmail(getParam("contact_email","")))
			$errorStr.="<li>".LA_ERROR_MSG21."</li>";
			
		if (getParam("contact_email","")=="")
			$errorStr.="<li>".LA_ERROR_MSG8."</li>";	
			
		if (getParam("contact_name","")=="")
			$errorStr.="<li>".LA_ERROR_MSG5."</li>";	
			
		if (!empty($set_ref) && !eregi("$set_ref",".$lServerRef[2]."))
			$errorStr.="<li>".LA_REF_FAILED."</li>";	
			
		if (getParam("contact_message","")=="")
			$errorStr.="<li>".LA_ERROR_MSG88."</li>";	
			
		if (getParam("validate_sign","")=="" && $set_img_validation)	
			$errorStr.="<li>".LA_IMG_VER_FAILED."</li>";

		if (($_SESSION["validate_sign_session"] <> $_REQUEST["validate_sign"] && $set_img_validation))		
			$errorStr.="<li>".LA_IMG_VER_FAILED."</li>";
		
		
		if (empty($errorStr))
		{
		
			// Person contacting the ad owne
			$lContactMessage=getParam("contact_message","");
		 	$lContactName=getParam("contact_name","");
		 	$lContactEmail=getParam("contact_email","");
		 	$tplContact->assign("message_sent",formatString(LA_SENT_MESSAGE,array("$lAdOwnerName")));
			
			$lContactArray=array("$lAdOwnerName","$lAdOwnerEmail","","$set_sitename","".getRemoteIp()."","http://$url/member_login.php"," http://$set_url/". linkDetail($lAdId),"$lAdTitle","$lContactName","$lContactEmail","$lContactMessage");
			$lSubject = formatString(LA_MAIL_CONTACT_SUBJECT, $lContactArray);
			$lBody = formatString(LA_MAIL_CONTACT_BODY, $lContactArray);
			//echo "SUBJECT: " . $lSubject . "<p>BODY: " . $lBody . "<p>Email to:" . $lContactEmail;
			if($lSubject=="")
			{
				$lSubject = getParam("subject","");			
			}
			//$lBody = $lContactMessage;
			//logMsg("JMR: email here","Estamos acá");
			sendEmail($lAdOwnerEmail, $lContactEmail, $lSubject, $lBody);
			
			$tplContact->assign("contact_ok",1);
			
			q("update $usr_tbl set user_contacted=0 where user_id=$lUserid AND (user_contacted IS NULL OR user_contacted='')"); // Counter is not correct if the inital data for some reason isn�t default 0 in MySql
			
			$sql_2 = "update $usr_tbl set user_contacted = user_contacted + 1 where user_id = $lUserid";
			$sql_resultupd = q ($sql_2);
			addToHistory(2,$lUserid,"","");
			
		}
		else 
		{	
			$tplContact->assign("contact_fail",$errorStr);
		}	
		
}
else
{
	
		// If user contacting owner is logged in, set logged in data details
		if ($_SESSION["valid_user"]!="") // If a userid is found, get name, email etc. of the person you try to contact
		{
			// Owner of this ad
			$lSqlSubmitter = "select user_name,user_email from $usr_tbl where user_id = ".round($_SESSION["valid_user"])." limit 1";
			$lResultSubmitter = q ($lSqlSubmitter);
			$lRowSubmitter = mysql_fetch_array($lResultSubmitter);
			$lSubmitterName=$lRowSubmitter["user_name"];
			$lSubmitterEmail=$lRowSubmitter["user_email"];
			
			$tplContact->assign("contact_name",strEnc($lSubmitterName));
			$tplContact->assign("contact_email", strEnc($lSubmitterEmail));
		}
}
$tplContact->display("contact.tpl");
?>
